<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="工号" prop="gonghao">
        <el-input v-model="dataForm.gonghao" placeholder="工号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="mima" v-if="!dataForm.id">
        <el-input v-model="dataForm.mima" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="教师姓名" prop="laoshixingming">
        <el-input v-model="dataForm.laoshixingming" placeholder="教师姓名"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="touxiang">
        <el-upload
            class="avatar-uploader"
            :action="url"
            :show-file-list="false"
            :on-success="touxiangSuccessHandle"
            :before-upload="beforeTouxiangUploadHandle">
          <img v-if="dataForm.touxiang" :src="dataForm.touxiang" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="性别" prop="xingbie">
        <el-radio-group v-model="dataForm.xingbie">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="所属班级" prop="banji">
        <el-select v-model="dataForm.banji" placeholder="请选择班级">
          <el-option
              v-for="item in banjiOptions"
              :key="item.id"
              :label="item.banjimingcheng"
              :value="item.banjimingcheng">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        gonghao: '',
        mima: '',
        laoshixingming: '',
        touxiang: '',
        xingbie: '男',
        mobile: '',
        banji: ''
      },
      dataRule: {
        gonghao: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        mima: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        laoshixingming: [
          { required: true, message: '教师姓名不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        banji: [
          { required: true, message: '所属班级不能为空', trigger: 'change' }
        ]
      },
      url: this.$http.adornUrl('/file/upload'),
      banjiOptions: []
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/laoshi/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.gonghao = data.laoshi.gonghao
              this.dataForm.laoshixingming = data.laoshi.laoshixingming
              this.dataForm.touxiang = data.laoshi.touxiang
              this.dataForm.xingbie = data.laoshi.xingbie
              this.dataForm.mobile = data.laoshi.mobile
              this.dataForm.banji = data.laoshi.banji
            }
          })
        }
        // 获取班级列表
        this.$http({
          url: this.$http.adornUrl('/banji/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 100
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.banjiOptions = data.page.list
          }
        })
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/laoshi/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'gonghao': this.dataForm.gonghao,
              'mima': this.dataForm.mima,
              'laoshixingming': this.dataForm.laoshixingming,
              'touxiang': this.dataForm.touxiang,
              'xingbie': this.dataForm.xingbie,
              'mobile': this.dataForm.mobile,
              'banji': this.dataForm.banji
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    // 头像上传成功
    touxiangSuccessHandle (response, file) {
      if (response && response.code === 0) {
        this.dataForm.touxiang = response.url
      } else {
        this.$message.error(response.msg)
      }
    },
    // 头像上传之前
    beforeTouxiangUploadHandle (file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG && !isPNG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
        return false
      }
      return true
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader {
  .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    &:hover {
      border-color: #409EFF;
    }
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
}
</style>